package com.openswimsoftware.orm.model.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.openswimsoftware.orm.model.Model;
import com.openswimsoftware.orm.model.list.query.ModelListDelete;
import com.openswimsoftware.orm.model.list.query.ModelListFind;
import com.openswimsoftware.orm.model.list.query.ModelListSave;

import android.content.Context;

public class ModelArrayList<E extends Model<E>> extends ArrayList<E> implements ModelList<E> {

	private static final long serialVersionUID = -2259751830972286786L;
	
	private Class<E> modelType;
	
	private boolean loaded = false;
	private boolean updated = false;
	private long parentId = -1;
	private String parentName;
	
	protected Context context;

	public ModelArrayList(Context context, Class<E> modelType) 
	{
		this.context = context;
		this.modelType = modelType;
	}
	
	public ModelArrayList(Context context, Class<E> modelType, int capacity) 
	{
		super(capacity);
		
		this.context = context;
		this.modelType = modelType;
	}
	
	public ModelArrayList(Context context, Class<E> modelType, ModelList<E> models) 
	{
		super(models);
		
		this.context = context;
		this.modelType = modelType;
	}
	
	public void setParentId(long parentId) 
	{
		this.parentId = parentId;
	}
	
	public long getParentId() 
	{
		return this.parentId;
	}
	
	public void setParentName(String name) 
	{
		this.parentName = name;
	}
	
	public String getParentName() 
	{
		return this.parentName;
	}
	
	public boolean getLoaded()
	{
		return this.loaded;
	}
	
	public void setLoaded(boolean loaded)
	{
		this.loaded = loaded;
	}
	
	public ModelList<E> find() 
	{
		return this.findQuery().execute();
	}
	
	public ModelListFind<E> findQuery()
	{
		return new ModelListFind<E>(context, modelType, this);
	}

	public ModelList<E> save() 
	{
		return this.saveQuery().execute();
	}
	
	public ModelListSave<E> saveQuery()
	{
		return new ModelListSave<E>(context, modelType, this);
	}

	public boolean delete() 
	{
		return this.deleteQuery().execute();
	}
	
	public ModelListDelete<E> deleteQuery()
	{
		return new ModelListDelete<E>(context, modelType, this);
	}
	
	public void load()
	{
		if (this.loaded == false && this.parentId > -1)
		{
			this.find();
		}
	}
	
	public boolean getUpdated()
	{
		return this.updated;
	}
	
	public boolean add(E object)
	{
		this.updated = true;
		
		return super.add(object);
	}
	
	public void add(int index, E object)
	{
		this.updated = true;
		
		super.add(index, object);
	}
	
	public boolean addAll(Collection<? extends E> collection)
	{
		this.updated = true;
		
		return super.addAll(collection);
	}
	
	public boolean addAll(int index, Collection<? extends E> collection)
	{
		this.updated = true;
		
		return super.addAll(index, collection);
	}
	
	public boolean contains(Object object)
	{
		this.load();
		
		return super.contains(object);
	}
	
	public E get(int index)
	{
		this.load();
		
		return super.get(index);
	}
	
	public int indexOf(Object object)
	{
		this.load();
		
		return super.indexOf(object);
	}

	public boolean isEmpty()
	{
		this.load();
		
		return super.isEmpty();
	}
	
	public Iterator<E> iterator()
	{
		this.load();
		
		return super.iterator();
	}
	
	public int lastIndexOf(Object object)
	{
		this.load();
		
		return super.lastIndexOf(object);
	}
	
	public E remove(int index)
	{
		this.load();
		
		return super.remove(index);
	}
	
	public boolean remove(Object object)
	{
		this.load();
		
		return super.remove(object);
	}
	
	public E set(int index, E object)
	{
		this.updated = true;
		
		return super.set(index, object);
	}
	
	public int size()
	{
		this.load();
		
		return super.size();
	}
	
	public E[] toArray(E[] contents)
	{
		this.load();
		
		return super.toArray(contents);
	}
	
	public Object[] toArray()
	{
		this.load();
		
		return super.toArray();
	}

}
